package com.houtai.controller.activity.apply;


import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.bean.TdActivity;
import com.houtai.bean.TdActivityApply;
import com.houtai.bean.dto.activity.apply.PageActivityApply;
import com.houtai.common.util.ExcelUtil;
import com.houtai.service.activity.ActivityService;
import com.houtai.service.activity.apply.ActivityApplyService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by lizhun on 2017/7/19.
 */
@Controller
@RequestMapping(value = {"v5"})
public class ActivityApplyController {
    @Resource
    private ActivityService activityService;

    @Resource
    private ActivityApplyService activityApplyService;
    /**
     * @Purpose  分页加载课程报名列表
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/activity/apply/list")
    @Authority(AuthorityType.Login)
    public ModelAndView user(HttpServletRequest request, PageActivityApply page){
        ModelAndView mv = new ModelAndView("activity/apply/list");
        mv.addObject("retInfo", activityApplyService.findTdActivityApply(page, request));
        mv.addObject("token", request.getParameter("token"));
        return mv;
    }

    /**
     * 导出报表
     * @return
     */
    @RequestMapping(value = "/activity/apply/export")
    @ResponseBody
    public void export(HttpServletRequest request,HttpServletResponse response,PageActivityApply page) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //excel标题
        String[] title = {"报名id","时间","用户id","姓名","电话"};
        TdActivity tdActivity = activityService.findTdActivityById(page.getActivity_id());
        List<TdActivityApply> datas = activityApplyService.findTdActivityApplyExport(page);
        //excel文件名
        String fileName = tdActivity.getActivity_name()+ "-" + "报名信息" + ".xls";
        //sheet名
        String sheetName = "报名列表";
        int size = datas.size();
        String[][] content = new String[size][100];
        for (int i = 0; i < size; i++) {
            content[i][0] = "" + datas.get(i).getApply_id();
            content[i][1] = format.format(datas.get(i).getAdd_time());
            content[i][2] = datas.get(i).getUser_id() + "";
            content[i][3] =  datas.get(i).getApply_name();
            content[i][4] =  datas.get(i).getPhone_no();



        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
